/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Attribute.java
 *
 * Created on 10-gen-2012, 18.35.42
 */

package sybel.pan;

import javax.swing.JOptionPane;

/**
 *
 * @author Stefano
 */
public class Attribute extends javax.swing.JPanel{

    /** Creates new form Attribute */
    public Attribute(TabBehaviour b) {
        tabBehaviour = b;
        initComponents();
    }

    public Attribute(TabBehaviour b,String name, String value) {
        tabBehaviour = b;
        initComponents();
        setNameValue(name, value);
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTextAttrName1 = new javax.swing.JTextField();
        jTextAttrValue1 = new javax.swing.JTextField();
        jPanelPlusButt = new javax.swing.JPanel();
        jButtplus = new javax.swing.JButton();
        jButtmin = new javax.swing.JButton();

        jPanelPlusButt.setMinimumSize(new java.awt.Dimension(5, 5));
        jPanelPlusButt.setPreferredSize(new java.awt.Dimension(5, 5));

        jButtplus.setText("+");
        jButtplus.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtplusActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanelPlusButtLayout = new javax.swing.GroupLayout(jPanelPlusButt);
        jPanelPlusButt.setLayout(jPanelPlusButtLayout);
        jPanelPlusButtLayout.setHorizontalGroup(
            jPanelPlusButtLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 41, Short.MAX_VALUE)
            .addGroup(jPanelPlusButtLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanelPlusButtLayout.createSequentialGroup()
                    .addGap(0, 0, Short.MAX_VALUE)
                    .addComponent(jButtplus)
                    .addGap(0, 0, Short.MAX_VALUE)))
        );
        jPanelPlusButtLayout.setVerticalGroup(
            jPanelPlusButtLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 23, Short.MAX_VALUE)
            .addGroup(jPanelPlusButtLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanelPlusButtLayout.createSequentialGroup()
                    .addGap(0, 0, Short.MAX_VALUE)
                    .addComponent(jButtplus)
                    .addGap(0, 0, Short.MAX_VALUE)))
        );

        jButtmin.setText("-");
        jButtmin.setMaximumSize(new java.awt.Dimension(41, 23));
        jButtmin.setMinimumSize(new java.awt.Dimension(41, 23));
        jButtmin.setPreferredSize(new java.awt.Dimension(41, 23));
        jButtmin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtminActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jTextAttrName1, javax.swing.GroupLayout.PREFERRED_SIZE, 195, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(22, 22, 22)
                .addComponent(jTextAttrValue1, javax.swing.GroupLayout.DEFAULT_SIZE, 418, Short.MAX_VALUE)
                .addGap(34, 34, 34)
                .addComponent(jButtmin, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanelPlusButt, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanelPlusButt, javax.swing.GroupLayout.DEFAULT_SIZE, 23, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jTextAttrName1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(jTextAttrValue1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(jButtmin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButtplusActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtplusActionPerformed
        tabBehaviour.newLineOfAttributes(null,null);
        tabBehaviour.validate();
    }//GEN-LAST:event_jButtplusActionPerformed

    private void jButtminActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtminActionPerformed
        minAction();
    }//GEN-LAST:event_jButtminActionPerformed

    public void minAction(){
        if(tabBehaviour.AttributesCount() > 1){
            tabBehaviour.deleteAttribute(this);
        } else {
            jTextAttrName1.setText(null);
            jTextAttrValue1.setText(null);
        }
        tabBehaviour.validate();
    }

    public void togglePlusButton(boolean b){
        if(b)
            jButtplus.setVisible(true);
        else
            jButtplus.setVisible(false);
    }

    public void setNameValue(String name, String value){
        jTextAttrName1.setText(name);
        jTextAttrValue1.setText(value);
    }

    /**
     * Metodo che restituisce il testo nel campo Name del pannello
     * @return nome dell'attributo
     */
    public String getName(){
        return jTextAttrName1.getText();
    }

    /**
     * Metodo che restituisce il testo nel campo Value del pannello
     * @return valore dell'attributo
     */
    public String getValue(){
        return jTextAttrValue1.getText();
    }

    /**
     * Metodo che verifica il corretto inserimento dei dati nei campi name e value
     * @return true se non ci sono errori nei dati presenti nei TextFields o se entrambi i campi sono vuoti -
     * @return false se il testo inserito nei campi è errato o manca uno dei due
     */
    public boolean checkfield() {
        if(jTextAttrName1.getText().isEmpty()){
            if(!jTextAttrValue1.getText().isEmpty()){
                JOptionPane.showMessageDialog(null, "The attribute's name assiociated with value '"+jTextAttrValue1.getText()+"' is incorrect");
                return false;
            }
            return true;
        }else{
            if(jTextAttrValue1.getText().isEmpty()){
                JOptionPane.showMessageDialog(null, "The Value of Attribute "+jTextAttrName1.getText()+" is incorrect");
                return false;
            }
            return true;
        }
    }

    /**
     * Metodo che restituisce true se entrambi i campi di questo jPanel sono vuoti
     */
    public boolean isEmpty(){
        if(jTextAttrName1.getText().isEmpty() && jTextAttrValue1.getText().isEmpty())
            return true;
        return false;
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtmin;
    private javax.swing.JButton jButtplus;
    private javax.swing.JPanel jPanelPlusButt;
    private javax.swing.JTextField jTextAttrName1;
    private javax.swing.JTextField jTextAttrValue1;
    // End of variables declaration//GEN-END:variables
    private TabBehaviour tabBehaviour;

}
